///////////////////////////////////////////////////
// Election Administration Polarization
// Plot of all mechanism RD coefficients
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

* Bring in the presidential election analysis data
use "$path/analysis_data/rd_election_analysis_data.dta", clear
keep if office=="pres"

* Define county conditions
sum res_rac, d
gen seg = res_rac > r(p50) if res_rac!=.
gen diverse = share_nhwhite<0.8 if share_nhwhite!=.
gen balanced = abs(lag_vs_dem - 0.5)<0.15 if lag_vs_dem!=.
gen competitive = abs(lag_state_vs_dem_pres-0.5)<0.05 if lag_state_vs_dem_pres!=.
gen determinative = lag_state_margin_pres<pop*2 if lag_state_margin_pres!=. & pop!=.
gen large_pop = pop>100000 if pop!=.

* All
rdrobust r_oos_state_year_vs_dem rv, vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id, est_command("rdrobust") new("B")

* Segregated
rdrobust r_oos_state_year_vs_dem rv, vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id, est_command("rdrobust") old("B")

* Diverse
rdrobust r_oos_state_year_vs_dem rv if diverse==1, vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if diverse==1, est_command("rdrobust") old("B")

* Balanced
rdrobust r_oos_state_year_vs_dem rv if balance==1, vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if balance==1, est_command("rdrobust") old("B")

* Large Population
rdrobust r_oos_state_year_vs_dem rv if large_pop==1, vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if large_pop==1, est_command("rdrobust") old("B")

* Competitive
rdrobust r_oos_state_year_vs_dem rv if competitive==1, vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if competitive==1, est_command("rdrobust") old("B")

* Determinative
rdrobust r_oos_state_year_vs_dem rv if determinative==1, vce(cluster election_id)
rdtab r_oos_state_year_vs_dem treat rv election_id if determinative==1, est_command("rdrobust") old("B")

* Plot the coefficients
matrix covs = B'
clear
svmat covs
rename (covs1 covs2) (b se)
gen upper = b + 1.96*se
gen lower = b - 1.96*se
gen subset = -_n
label define subset_lab -1 "All Counties" -2 "Segregated Counties" -3 "Diverse Counties" -4 "Balanced Counties" -5 "Large-Population Counties" -6 "Competitive States" -7 "Determinative Counties" 
label values subset subset_lab 
twoway (rcap upper lower subset, horizontal lc(gs4)) ///
	(scatter subset b, mc(gs2)), ///
	xli(0, lp(dash) lc(gs10)) ///
	xti("Effect on Dem Pres Vote Share") yti("") ///
	ysc(r(-7.5 -0.5)) ylab(-7(1)-1, valuelabel angle(horizontal) notick nogrid) ///
	legend(off) ///
	graphregion(color(white))
graph export "$path/output/rd_plot_mechanisms.pdf", replace

